#!/usr/bin/env bash

export ____avrn_mv=0

____avMove() {
  if [ -z "$1" ]; then
    return
  fi

  if [ -z "$2" ]; then
    return
  fi

  if [ "$1" != "$2" ]; then
    if [ "${____avrn_mv}" = "1" ]; then
      mv "$1" "$2"
    fi
    echo "\"$2\" <- \"$1\""
  fi
}

____avReplDir() {
  if [ -z "$1" ]; then
    return
  fi

  if [ -z "$2" ]; then
    return
  fi

  echo "dir: \"$2\""

  findExp=$1
  seekExp=$2
  replExp=$3

  OLD_IFS=$IFS
  IFS=$'\n'
  for n in $(ls | grep -E "${findExp}"); do
    nn=$(echo $n | sed "s/${seekExp}/${replExp}/g")
    ____avMove "${n}" "${nn}"
  done
  IFS=$OLD_IFS
}

____avReplFn() {
  if [ -z "$1" ]; then
    return
  fi

  if [ -z "$2" ]; then
    return
  fi

  echo "name: \"$2\""

  findExp=$1
  seekExp=$2
  replExp=$3

  OLD_IFS=$IFS
  IFS=$'\n'
  for n in $(find . -type f -name "${findExp}"); do
    nn=$(echo $n | sed "s/${seekExp}/${replExp}/g")
    ____avMove "${n}" "${nn}"
  done
  IFS=$OLD_IFS
}

____avReplTs() {
  echo "name: \".com .TS\""
  find . -type f -name '*.com .nfo' -exec rm {} \;
  OLD_IFS=$IFS
  IFS=$'\n'
  for n in $(find . -name '*.com .TS'); do
    dn=$(dirname $n)
    bn=${dn##*/}
    nn=${dn}/${bn}.ts
    ____avMove "${n}" "${nn}"
  done

  for n in $(find . -name '* .TS'); do
    nn=$(echo $n | sed "s/ .TS$/.ts/g")
    ____avMove "${n}" "${nn}"
  done
  IFS=$OLD_IFS
}

____cleanCode() {
  OLD_IFS=$IFS
  IFS=$'\n'

  exp='(91|MKY-)?[a-zA-Z]{2,5}-?[0-9]{2,6}(-[0-9]{1,3})?\s*'

  echo "code: dir"
  for n in $(ls | grep -E ${exp}); do
    prefix=$(echo $n | grep -Eo $exp)
    code=$(echo $prefix | sed 's/ //g')
    nn=$(echo $n | sed "s/${prefix}/${code}./g; s/\.\./\./g")
    ____avMove "${n}" "${nn}"
  done

  echo "code: name"
  for n in $(find . -type f | grep -E $exp); do
    dn=$(dirname $n)
    bn=${n##*/}
    prefix=$(echo $bn | grep -Eo $exp)
    if [ -n "$prefix" ]; then
      code=$(echo $prefix | sed 's/ //g')
      bn=$(echo $bn | sed "s/${prefix}/${code}./g; s/\.\./\./g")
      nn=${dn}/${bn}
      ____avMove "${n}" "${nn}"
    fi
  done

  echo "code: name uni"
  exp='(91)?[a-zA-Z]{2,5}-[0-9]{2,6}'
  for n in $(find . -type f | grep -E $exp); do
    dn=$(dirname $n)
    bn=${n##*/}
    prefix=$(echo $bn | grep -Eo ${exp})
    if [ -n "$prefix" ]; then
      code=$(echo $prefix | sed 's/-//g')
      bn=$(echo $bn | sed "s/${prefix}/${code}/g")
      nn=${dn}/${bn}
      ____avMove "${n}" "${nn}"
    fi
  done

  IFS=$OLD_IFS
}

____avrn() {
  find . -type f -name '*.js' -exec rm {} \;
  ____avReplDir 'ThZu.Cc' '\[ThZu.Cc\]'
  ____avReplDir '麻豆传媒出品' '麻豆传媒出品'
  ____avReplDir '天美传媒' '天美传媒'
  ____avReplDir '蜜桃传媒' '蜜桃传媒'
  ____avReplDir '星空传媒' '星空传媒'
  ____avReplDir '蜜桃影像传媒原版' '蜜桃影像传媒原版'
  ____avReplDir '麻豆传媒' '麻豆传媒'
  ____avReplDir '蜜桃影像原版' '蜜桃影像原版'
  ____avReplDir '果冻传媒' '果冻传媒'
  ____avReplDir '精东影业' '精东影业'
  ____avReplDir '麻豆傳媒映畫原版' '麻豆傳媒映畫原版'
  ____avReplDir '猫爪传媒' '猫爪传媒'
  ____avReplDir '兔子先生' '兔子先生'
  ____avReplDir '涩会传媒' '涩会传媒'
  ____avReplDir '色控' '色控'
  ____avReplDir '肉肉传媒' '肉肉传媒'
  ____avReplDir '麻 豆 傳 媒 最 新 出 品' '麻 豆 傳 媒 最 新 出 品'

  local MVP="JD MAD MD MDWP MDX MKY-HM MKY-NS MM MMZ MSD PH PMC PMX PNC SH TM TWAV TZ RR GDCM DB LTV OS LAX JDSY WY"
  for n in $(echo $MVP); do
    ____avReplDir "$n-" "$n-" "$n"
  done

  ____avReplFn '*ThZu.Cc*' '\[ThZu.Cc\]'
  ____avReplFn '*guochan2048.com-*' 'guochan2048.com-'
  ____avReplFn '*麻豆传媒出品*' '麻豆传媒出品'
  ____avReplFn '*天美传媒*' '天美传媒'
  ____avReplFn '*蜜桃传媒*' '蜜桃传媒'
  ____avReplFn '*星空传媒*' '星空传媒'
  ____avReplFn '*蜜桃影像传媒原版*' '蜜桃影像传媒原版'
  ____avReplFn '*麻豆传媒*' '麻豆传媒'
  ____avReplFn '*蜜桃影像原版*' '蜜桃影像原版'
  ____avReplFn '*果冻传媒*' '果冻传媒'
  ____avReplFn '*精东影业*' '精东影业'
  ____avReplFn '*麻豆傳媒映畫原版*' '麻豆傳媒映畫原版'
  ____avReplFn '*猫爪传媒*' '猫爪传媒'
  ____avReplFn '*兔子先生*' '兔子先生'
  ____avReplFn '*色控*' '色控'
  ____avReplFn '*肉肉传媒*' '肉肉传媒'
  ____avReplFn '*麻 豆 傳 媒 最 新 出 品*' '麻 豆 傳 媒 最 新 出 品'

  for n in $(echo $MVP); do
    ____avReplFn "*$n-*" "$n-" "$n"
  done

  ____avReplTs
  ____avReplDir '^ ' '^ '
  ____avReplFn ' *' '\/ ' '\/'

  ____cleanCode
}

avrnt() {
  export ____avrn_mv=0
  ____avrn
}

avrn() {
  export ____avrn_mv=1
  ____avrn
}

____mvrn() {
  ____avReplDir '阳光电影www.ygdy8.com.' '阳光电影www.ygdy8.com.'
  ____avReplDir '.mkv' '.mkv'
  ____avReplDir '.mp4' '.mp4'
  ____avReplDir '(1)' '(1)'
  ____avReplFn '阳光电影www.ygdy8.com.*' '阳光电影www.ygdy8.com.'
}

mvrn() {
  export ____avrn_mv=1
  ____mvrn
}

mvrnt() {
  export ____avrn_mv=0
  ____mvrn
}
